DATASET ACTIVATE DataSet1.
* DHS Calendar Tutorial - Example 3.
* whether woman used family planning at any point in first year after most recent birth.

* variable ppfp (post-partum family planning) will be
* 0 = No method used in first 12 months after birth
* 1 = Traditional method used in first 12 months after birth
* 2 = Modern method used in first 12 months after birth.
* restricted to women whose most recent birth is at least 12 months before interview
* back to five years before interview.
* birth1_5=1 if the woman meets these criteria.

* download the model dataset for individual women's recode: "ZZIR62FL.SAV"
* the model datasets are available at http://dhsprogram.com/data/download-model-datasets.cfm .

* change to a working directory where the data are stored
* or add the full path to the 'get file' command below.
cd "C:\Data\DHS_model".

* open the dataset to use, and just keep the variables we are going to use.
get file="ZZIR62FL.SAV" / keep vcal$1 v000 v005 v007.


* Step 3.1.
* remove the leading blanks for the months after the interview.
string trim_cal (a80).
compute trim_cal=ltrim(vcal$1).

* search for the last birth in the calendar.
compute lb=char.index(trim_cal,"B").
* eligible if most recent birth is between 13 months ago and 60 months ago
* equivalent to months 12-59 preceding the survey when month of interview is month 0.
compute birth1_5=(lb >= 13 & lb <= 60).

* extract the postpartum period after the birth.
string pp1 (a80).
if (birth1_5 = 1) pp1 = char.substr(trim_cal,1,lb-1).
execute.


* Step 3.2.
* Macro to reverse a string.
define !ReverseStr(!positional !tokens(1) /!positional !tokens(1))
* first parameter is old variable, second is new variable.
+ compute !2 = !1.
+ string #a (A1).
+ compute #l = length(rtrim(!2)).
+ loop #i = 1 to #l/2.
+   compute #j = #l - #i + 1.
+   compute #a = char.substr(!2,#i,1).
+   compute substr(!2,#i,1) = char.substr(!2,#j,1).
+   compute substr(!2,#j,1) = #a.
+ end loop.
+ execute.
!enddefine.

string pp1_rev (a80).
* reverse the string for the period after the birth
* so we are going forward in time from the birth.
* limit to women whose most recent birth is at least 12 months before interview.
!ReverseStr pp1 pp1_rev.
* and then extract the first 12 months.
* limit to women whose most recent birth is at least 12 months before interview.
string postbirth (a12).
if (birth1_5 = 1) postbirth=char.substr(pp1_rev,1,12).


* Step 3.3.
* see if anything happened in this 12 month period other than non-use of contraception.
do if (birth1_5 = 1).
+ compute used_month = 1.
+ loop if (used_month <= 12 & char.substr(postbirth,used_month,1) = "0").
+   compute used_month = used_month+1.
+ end loop.
end if.
* if no birth in the period 12-59 months preceding the survey (birth1_5 <> 1) then
* reset used_month to 0 to facilitate later steps.
if (birth1_5 = 0 | used_month > 12) used_month = 0.

* get the method code for the method used following the pregnancy.
string method_used (a1).
if (used_month > 0) method_used = char.substr(postbirth,used_month,1).
* something was found, but it might be a pregnancy (or possibly a termination),
* if so don't count this. Births are always preceded by pregnancy,
* but a termination in month 1 would not have a P preceding it.
do if (used_month > 0).
+ if (char.index("PT",method_used) > 0) used_month = 0.
+ if (used_month = 0) method_used = " ".
end if.


* Step 3.4.
* generate postpartum family planning variable, initially set to 0.
if (birth1_5 = 1) ppfp=0.
* update ppfp if used a method.
if (used_month > 0) ppfp = 1.
* search the 12 months after birth for one of the modern methods.
* the list of codes below (in the 'strpos' function) are survey specific
* and should be adapted for each survey.
* in particular codes "E", "M", and "S" may have been traditional methods in older surveys,
* but are now standard codes for Emergency contraception, Other modern methods,
* and Standard days method.
* also note that "L" (LAM) could be excluded because it is only valid within 6 months after birth.
do if (used_month > 0).
+ if (char.index("1234567LNCFEMS",method_used) > 0) ppfp = 2.
end if.
execute.

* label the ppfp variable.
variable labels ppfp "Used modern method within 12 months of birth".
value labels ppfp 0 "no method used" 1 "traditional method used" 2 "modern method used".


* Step 3.5.
* weight the data and tabulate.
compute wt=v005/1000000.
weight by wt.
filter by birth1_5.
frequencies variables=ppfp.


Frequencies
Notes
|-----------------------------------------------------|---------------------------------------------------|
|Output Created                                       |31-AUG-2017 01:15:22                               |
|-----------------------------------------------------|---------------------------------------------------|
|Comments                                             |                                                   |
|----------------------|------------------------------|---------------------------------------------------|
|Input                 |Filter                        |birth1_5                                           |
|                      |------------------------------|---------------------------------------------------|
|                      |Weight                        |wt                                                 |
|                      |------------------------------|---------------------------------------------------|
|                      |Split File                    |<none>                                             |
|                      |------------------------------|---------------------------------------------------|
|                      |N of Rows in Working Data File|3003                                               |
|----------------------|------------------------------|---------------------------------------------------|
|Missing Value Handling|Definition of Missing         |User-defined missing values are treated as missing.|
|                      |------------------------------|---------------------------------------------------|
|                      |Cases Used                    |Statistics are based on all cases with valid data. |
|-----------------------------------------------------|---------------------------------------------------|
|Syntax                                               |frequencies variables=ppfp.                        |
|----------------------|------------------------------|---------------------------------------------------|
|Resources             |Processor Time                |00:00:00.02                                        |
|                      |------------------------------|---------------------------------------------------|
|                      |Elapsed Time                  |00:00:00.01                                        |
|---------------------------------------------------------------------------------------------------------|

Statistics
ppfp Used modern method within 12 months of birth 
|-|-------|----|
|N|Valid  |2930|
| |-------|----|
| |Missing|0   |
|--------------|

ppfp Used modern method within 12 months of birth
|-----------------------------|---------|-------|-------------|------------------|
|                             |Frequency|Percent|Valid Percent|Cumulative Percent|
|-----|-----------------------|---------|-------|-------------|------------------|
|Valid|no method used         |2634     |89.9   |89.9         |89.9              |
|     |-----------------------|---------|-------|-------------|------------------|
|     |traditional method used|25       |.8     |.8           |90.7              |
|     |-----------------------|---------|-------|-------------|------------------|
|     |modern method used     |272      |9.3    |9.3          |100.0             |
|     |-----------------------|---------|-------|-------------|------------------|
|     |Total                  |2930     |100.0  |100.0        |                  |
|--------------------------------------------------------------------------------|

filter off.